package com.joint.web.action.mp;

import com.joint.base.bean.EnumManage;
import com.joint.base.entity.Users;
import com.joint.base.service.AuthCodeService;
import com.joint.base.service.UsersService;
import com.joint.web.action.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by dqf on 2015/4/20.
 */
@Controller("mpUsers")
@RequestMapping("/mp")
public class AjaxUsersAction extends BaseController {
    @Resource
    private UsersService usersService;
    @Resource
    private AuthCodeService authCodeService;

    private Users users;
    private String oldPassword;
    private String newPassword;
    private String confirmPassword;
    private String authcode;
    private String mobile;
    private String openId;

    @RequestMapping(value = "/ajax-users/input",method = RequestMethod.GET)
    public String input(){
        return "/mp/ajax-users_input";
    }

    public String resetinput(){return "reset";}

    public String experience(){
        return "experience";
    }

    public Map<String,Object> sendAuthCode(){
        return ajaxResult(usersService.sendAuthCode(mobile, EnumManage.AuthCodeEnum.passwordForget,1,null));
    }

    public Map<String,Object> save(){

        if (!confirmPassword.equals(newPassword)){
            return ajaxHtmlAppResult(400, "两次输入的密码不一致！", null);
        }

        if (StringUtils.isEmpty(newPassword)){
            return ajaxHtmlAppResult(400, "密码不能为空！",null);
        }

        Users users = usersService.getUsersByMobile(mobile);
        if(!authCodeService.isValid(users.getMobile(), authcode)){
            return ajaxHtmlAppResult(400, "验证码错误！",null);
        }

        try{
            usersService.ResetPass(users, newPassword);
        }catch (Exception e){
            return ajaxHtmlAppResult(400, "密码重置失败！", null);
        }

        return ajaxHtmlAppResult(200, "密码重置成功！",null);
    }

    @RequestMapping(value = "/ajax-reset",method = RequestMethod.POST)
    public String reset() {
        users = usersService.getLoginInfo();
        if (!StringUtils.equals(oldPassword, users.getPassword())){
            return ajaxHtmlCallback("100", "输入原密码错误！");
        }
        try {
            usersService.ResetPass(users, newPassword);
        }catch (Exception e){
            return ajaxHtmlCallback("400", "密码重置失败！");
        }
        return ajaxHtmlCallback("200", "密码重置成功！");
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getAuthcode() {
        return authcode;
    }

    public void setAuthcode(String authcode) {
        this.authcode = authcode;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }
}
